package swimv2.session;

import java.util.ArrayList;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;

import swimv2.entity.*;
import swimv2.session.*;

/**
 * Session Bean implementation class AbilityManagerBean
 */
@Stateless
@RemoteBinding( jndiBinding = "IAbilityManagerRemote" )
public class AbilityManagerBean implements IAbilityManagerRemote {
	@PersistenceContext(unitName = "swimv2")
	protected EntityManager manager;

    /**
     * Default constructor. 
     */
    public AbilityManagerBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public boolean addAbility(Integer Id_user, String ab) {
		if(AlreadyHave(ab, Id_user)){
			HasAbility h= new HasAbility();
				
			h.setUser(manager.find(User.class, Id_user));
			
			Query q= manager.createQuery("SELECT a FROM Ability a WHERE a.Name = :nome");
			q.setParameter( "nome", ab );	
			
			h.setAbility((Ability)q.getSingleResult());
			
			manager.persist(h);
			return true;
			
		}
		return false;
			
	}

	@Override
	public boolean createAbility(String nome) {
		if(AlreadyExist(nome)){
			return false;
		}
		else{
			Ability a= new Ability();
			a.setName(nome);
			manager.persist(a);
			
			Query q = manager.createQuery("SELECT a FROM AbilityRequest a WHERE a.nome = :nome");
			q.setParameter( "nome", nome );	
			List<AbilityRequest> ab= (List<AbilityRequest>)q.getResultList();
						
			manager.remove(manager.find(AbilityRequest.class,ab.get(0).getId() ));
			
			return true;
		}
	}

	@Override
	public boolean requestAbility(Integer Id_user, String nome) {
		
		AbilityRequest a= new AbilityRequest();
			
		a.setName(nome);
		a.setApplicant(manager.find(User.class, Id_user));
		
		manager.persist(a);
		
		return false;
	}

	@Override
	public boolean removeAbility( String abilita, Integer Id_user) {
		
		Query q= manager.createQuery("SELECT a FROM Ability a WHERE a.Name = :nome");
		q.setParameter( "nome", abilita );	
		Ability a= (Ability)q.getSingleResult();
		
		q = manager.createQuery("SELECT h FROM HasAbility h WHERE h.ability = :idA and h.user = :idU");
		q.setParameter( "idA", a );
		q.setParameter( "idU", manager.find(User.class, Id_user) );	
		
		manager.remove((HasAbility)q.getSingleResult());
		return true;
	}

	@Override
	public List<Ability> listAbility() {
		
		List<Ability> lista= new ArrayList<Ability>();
		
		Query q = manager.createQuery("SELECT a FROM Ability a ");
		
		return (List<Ability>)q.getResultList();
	}

	@Override
	public Ability searchAbility(String nome) {
		Query q= manager.createQuery("SELECT a FROM Ability a WHERE a.Name = :nome");
		q.setParameter( "nome", nome );	

		return (Ability)q.getSingleResult();
	}

	@Override
	public List<Ability> listAbility(Integer id_utente) {
		Query q= manager.createQuery("SELECT h FROM HasAbility h WHERE h.user.id = :idU");
		q.setParameter( "idU", id_utente );	
		List<HasAbility> res= (List<HasAbility>)q.getResultList();
		
		List<Ability> AbL= new ArrayList<Ability>();
		
		for(int i=0; i<res.size(); i++)
			AbL.add(manager.find(Ability.class, res.get(i).getId() ));
			
		return AbL;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AbilityRequest> listRequestedAbility() {
		return (List<AbilityRequest>)manager.createQuery("SELECT a FROM AbilityRequest a")
				.getResultList();
	}
	
	private boolean AlreadyExist(String nome){
		Query q = manager.createQuery("SELECT a FROM Ability a WHERE a.Name = :nome");
		q.setParameter( "nome", nome );
		List<Ability> results = (List<Ability>)q.getResultList();
		try{
			if(results.isEmpty())
			{
				return false;
			}
			else 
			{
				return true;
			}
		}
		catch(NullPointerException ex)
		{
			return true;
		}
	};
	
	private boolean AlreadyHave(String nome, int User_Id){
		
		Query q = manager.createQuery("SELECT a FROM Ability a WHERE a.Name = :nome");
		q.setParameter( "nome", nome );
		List<Ability> results = (List<Ability>)q.getResultList();
		
		try{
			if(results.isEmpty())
			{
				return false;
			}
			else 
			{

				q = manager.createQuery("SELECT h FROM HasAbility h WHERE h.ability = :idA and h.user = :idU");
				q.setParameter( "idA", results.get(0) );
				q.setParameter( "idU", manager.find(User.class, User_Id) );
				HasAbility res = (HasAbility)q.getSingleResult();
				return true;
			}
		}
		catch(NullPointerException ex)
		{
			return true;
		}
	}

	@Override
	public List<AbilityRequest> listRequestedAbility(int i) {
		return (List<AbilityRequest>)manager.createQuery("SELECT a FROM AbilityRequest a WHERE a.applicant.id = :id")
				.setParameter("id", i)
				.getResultList();
	};
}
